package com.binge.test2;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;

@Slf4j
public class CoreTest {

    public static void main(String[] args) {

    }

    public static String replace(String input, String newHost, String targetDomain, String newDomain) {
        StringBuilder output = new StringBuilder();
        int len = input.length();
        int start = 0;

        while (start < len) {
            int httpIndex = input.indexOf("http://", start);
            int httpsIndex = input.indexOf("https://", start);
            int domainIndex = input.indexOf(targetDomain, start);

            int nextIndex = findMinPositive(httpIndex, httpsIndex, domainIndex);
            String protocol = "";

            if (nextIndex == httpIndex) {
                protocol = "http://";
            } else if (nextIndex == httpsIndex) {
                protocol = "https://";
            }

            if (nextIndex >= 0) {
                output.append(input, start, nextIndex);

                if (!protocol.isEmpty()) {  // 是一个 URL
                    output.append(protocol);
                    output.append(newHost);
                    int endOfUrl = findEndOfUrl(input, nextIndex + protocol.length());
                    start = input.indexOf('/', nextIndex + protocol.length());
                    if (start < 0 || start > endOfUrl) {
                        start = endOfUrl;
                    }
                } else {  // 是一个独立域名
                    output.append(newDomain);
                    start = nextIndex + targetDomain.length();
                }
            } else {
                output.append(input, start, len);
                break;
            }
        }

        return output.toString();
    }

    private static int findMinPositive(int... values) {
        int min = Integer.MAX_VALUE;
        for (int value : values) {
            if (value >= 0 && value < min) {
                min = value;
            }
        }
        return (min == Integer.MAX_VALUE) ? -1 : min;
    }

    private static int findEndOfUrl(String input, int start) {
        int len = input.length();
        for (int i = start; i < len; i++) {
            char ch = input.charAt(i);
            if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '\"' || ch == '\'' || ch == '<' || ch == '>') {
                return i;
            }
        }
        return len;
    }







    static class Parent{

         static int a = 1;

        public Parent() {
            a = 4;
            print();
        }

        public void print(){
            System.out.println("Parent print:" + a);
        }

    }


    static class  Child extends Parent{
         int a = 2;

        public Child() {
            super();
            this.a = 3;
            print();
        }

        @Override
        public void print(){
            System.out.println("child print:" + a);
        }
    }


    @Test
    public void testCC() {

        Parent child = new Child();

    }


    @Test
    public void generateNumbers() {
        int len = 6;
        int count = 10;
        Long prefix = 11L;
        Set<Long> numbers = new HashSet<>();
        for (; ; ) {
            if (numbers.size() >= count) {
                break;
            }
            // 生成随机数
            long number = (long) (Math.random() * Math.pow(10, len));
            if (prefix != null) {
                number = prefix * (long) Math.pow(10, len) + number;
            }
            numbers.add(number);
        }
       log.info("numbers:{}", numbers);
    }


    @Test
    public void testPAndC() throws NoSuchFieldException, IllegalAccessException {
        C c = new C();
        log.info("c:{}", c);
        //Class<? super C> superclass = C.class.getSuperclass();
        //log.info("superclass:{}", superclass);
        Field name = P.class.getDeclaredField("name");
        name.setAccessible(true);

        Object o = name.get(c);

        log.info("o:{}", o);

    }
}

